package com.yuyou.tas.util.office.edit;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

public class Token {

    public  enum Permission{
        READ,WRITE,PRINT,DOWNLOAD,DELETE,COPY
    }

    private static Map<String,String> appId2SecretKey = new HashMap<>();

    static {
        appId2SecretKey.put("0001","3vtBxt8XXnig1i0q");
    }

    public static class TokenBody{

        private String resourceId ;

        private Permission[] permissions;

        private Date expire ;

        public TokenBody(String resourceId, Permission[] permissions,Date expire) {
            this.resourceId = resourceId;
            this.permissions = permissions;
            this.expire = expire;
        }

        public String getResourceId() {
            return resourceId;
        }

        public void setResourceId(String resourceId) {
            this.resourceId = resourceId;
        }

        public Permission[] getPermissions() {
            return permissions;
        }

        public void setPermissions(Permission[] permissions) {
            this.permissions = permissions;
        }

        public Date getExpire() {
            return expire;
        }

        public void setExpire(Date expire) {
            this.expire = expire;
        }

        public boolean hasPermission(Token.Permission permission ) {
            if( this.getPermissions() == null  ){
                return false;
            }
            for(Token.Permission p : this.getPermissions()){
                if(p == permission ){
                    return true ;
                }
            }
            return false;
        }
    }

    public static String generate(String appId ,String resourceId,Date expire ,Permission ... permissions ){

        if(appId == null || resourceId == null || expire == null || permissions == null ){
            throw new NullPointerException();
        }

        TokenBody tokenBody = new TokenBody(resourceId,permissions,expire);
        try {
            return Base64.encodeBase64URLSafeString((appId +":" +AES.encrypt( JSON.toJSONString(tokenBody),appId2SecretKey.get(appId))).getBytes(Charsets.UTF_8) ) ;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static TokenBody parseAndValid(String token ){

        token = new String( Base64.decodeBase64(token),Charsets.UTF_8 );

        String[] tokens = StringUtils.split(token,":",2 );
        String tokenInfo ;
        try {
            tokenInfo = AES.decrypt(tokens[1],appId2SecretKey.get(tokens[0]) );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        TokenBody tokenBody = JSON.parseObject(tokenInfo, TokenBody.class);

        if(tokenBody.getExpire().before(new Date())){
            throw new RuntimeException("Token is expired");
        }
        return  tokenBody;
    }


}
